package edu.nf.project.services.employeeAndCustomerInformation.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import edu.nf.project.entitys.entity.employeeAndCustomerInformation.relevancy.CustomerInfo_Coupon;
import edu.nf.project.mappers.employeeAndCustomerInformation.relevancy.CustomerInfo_CouponMapper;
import edu.nf.project.services.employeeAndCustomerInformation.CustomerInfo_CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerInfo_CouponServiceImpl implements CustomerInfo_CouponService {
    @Autowired
    private CustomerInfo_CouponMapper customerInfoCouponMapper;

    @Override
    public Boolean useCoupon(Integer customerId, Integer couponId) {
        LambdaQueryWrapper<CustomerInfo_Coupon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerInfo_Coupon::getCustomerId, customerId)
                .eq(CustomerInfo_Coupon::getCouponId, couponId)
                .orderByAsc(CustomerInfo_Coupon::getDeadlineTime)
                .last("limit 1");
        CustomerInfo_Coupon coupon = customerInfoCouponMapper.selectOne(wrapper);
        if (coupon != null) {
            // 直接用主键删除，效率高且不会锁全表
            return customerInfoCouponMapper.deleteById(coupon.getCcId()) > 0;
        }
        return false;
    }
}
